<template>
  <el-dialog
    :title="!dataForm.id ? '添加组' : '修改组'"
    :close-on-click-modal="false"
    @close="dialogClose"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="组名" prop="name">
        <el-input v-model="dataForm.name" placeholder="组名"></el-input>
      </el-form-item>
      <el-form-item label="排序值" prop="sort">
<!--        <el-input v-model="dataForm.sort" placeholder="排序值"></el-input>-->
        <el-select v-model="dataForm.sort" placeholder="请选择">
          <el-option
            label="0"
            :value="0">
          </el-option>
          <el-option
            label="1"
            :value="1">
          </el-option>
          <el-option
            label="2"
            :value="2">
          </el-option>
          <el-option
            label="3"
            :value="3">
          </el-option>
          <el-option
            label="4"
            :value="4">
          </el-option>
          <el-option
            label="5"
            :value="5">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="说明" prop="description">
        <el-input v-model="dataForm.description" placeholder="说明"></el-input>
      </el-form-item>
      <el-form-item label="组图标" prop="icon">
        <el-input v-model="dataForm.icon" placeholder="组图标"></el-input>
      </el-form-item>
      <el-form-item label="所属类别" prop="categoryId">
        <!--        <el-input v-model="dataForm.categoryId" placeholder="所属类别id"></el-input>-->
        <el-cascader
          filterable
          placeholder="请输入类别名"
          v-model="cascadedCategoryId"
          :options="categories"
          :props="props"
        ></el-cascader>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      categories: [],
      cascadedCategoryId: [],
      props: {
        value: "id",
        label: "name",
        children: "childrenCategories"
      },
      visible: false,
      dataForm: {
        id: 0,
        name: '',
        sort: '',
        description: '',
        icon: '',
        categoryId: 0
      },
      dataRule: {
        name: [
          {required: true, message: '组名不能为空', trigger: 'blur'}
        ],
        sort: [
          {required: true, message: '排序值不能为空', trigger: 'blur'}
        ],
        description: [
          {required: true, message: '说明不能为空', trigger: 'blur'}
        ],
        icon: [
          {required: true, message: '组图标不能为空', trigger: 'blur'}
        ],
        categoryId: [
          {required: true, message: '所属类别id不能为空', trigger: 'blur'}
        ]
      }
    }
  }, created() {
    this.getCategories()
  },
  methods: {
    dialogClose() {
      this.cascadedCategoryId = []
    },
    getCategories() {
      this.$http({
        url: this.$http.adornUrl('/commodity/category/list/tree'),
        method: 'get'
      }).then(({data}) => {
        this.categories = data.data
      })
    },
    init(id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/commodity/attrGroup/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.name = data.attrGroup.name
              this.dataForm.sort = data.attrGroup.sort
              this.dataForm.description = data.attrGroup.description
              this.dataForm.icon = data.attrGroup.icon
              this.dataForm.categoryId = data.attrGroup.categoryId
              this.cascadedCategoryId = data.attrGroup.cascadedCategoryId
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/commodity/attrGroup/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'name': this.dataForm.name,
              'sort': this.dataForm.sort,
              'description': this.dataForm.description,
              'icon': this.dataForm.icon,
              'categoryId': this.cascadedCategoryId[this.cascadedCategoryId.length - 1]
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
